package com.sweets.look.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.webkit.WebView;

import java.net.URISyntaxException;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;

import com.sweets.look.R;
import com.sweets.look.constant.Constants;
import com.sweets.look.ui.activity.BrowserActivity;

public class IntentUtils {
    private final static String TAG = IntentUtils.class.getSimpleName();
    private static final Pattern ACCEPTED_URI_SCHEMA = Pattern.compile("(?i)((?:http|https|file):\\\\/\\\\/|(?:inline|data|about|javascript):|(?:.*:.*@))(.*)");

    private final Activity mActivity;


    public IntentUtils(BrowserActivity controller) {
        super();
        this.mActivity = controller.getActivity();
    }

    public static boolean canOpenUseLocalApp(Activity activity, String url) {
        Intent v1;
//      ComponentName v7 = null;
        boolean v3 = false;
        if (url != null) {
            LogUtils.w(TAG, "check whether can open: " + url);
            try {
                v1 = Intent.parseUri(url, 1);
            } catch (URISyntaxException v0) {
                return v3;
            }

            v1.addCategory("android.intent.category.BROWSABLE");
            v1.setComponent(null);
            if (Constants.API >= 15) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
                    v1.setSelector(null);
                }
            }

            if (activity.getPackageManager().resolveActivity(v1, 0) == null) {
                return v3;
            }

            String v2 = activity.getPackageManager().resolveActivity(v1, 0).activityInfo.packageName;
            if (v2 == null) {
                return v3;
            }

            if (v2.equals(activity.getPackageName())) {
                return v3;
            }

            v3 = true;
        }

        return v3;
    }

    public static boolean isSpecializedHandlerAvailable(Activity activity, Intent intent) {

        List<ResolveInfo> v1 = activity.getPackageManager().queryIntentActivities(intent,
                PackageManager.GET_RESOLVED_FILTER);//64
        if (v1 != null && !v1.isEmpty()) {
            Iterator<ResolveInfo> v5 = v1.iterator();

            while (v5.hasNext()) {
                IntentFilter v0 = v5.next().filter;
                if (v0 == null) {
                    continue;
                }

                if (v0.countDataAuthorities() == 0) {
                    continue;
                }

                if (v0.countDataPaths() == 0) {
                    continue;
                }

                return true;
            }
        }

        return false;
    }

    public static Intent newEmailIntent(Context context, String address, String subject, String body,
                                        String cc) {
        Intent v0 = new Intent("android.intent.action.SEND");
        v0.putExtra("android.intent.extra.EMAIL", new String[]{address});
        v0.putExtra("android.intent.extra.TEXT", body);
        v0.putExtra("android.intent.extra.SUBJECT", subject);
        v0.putExtra("android.intent.extra.CC", cc);
        v0.setType("message/rfc822");
        return v0;
    }

    public static void openUseLocalApp(Activity activity, String url) {
        Intent v1;

        try {
            v1 = Intent.parseUri(url, 1);
        } catch (URISyntaxException v0) {
            return;
        }

        v1.addCategory("android.intent.category.BROWSABLE");
        v1.setComponent(null);
        if (Constants.API >= 15) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
                v1.setSelector(null);
            }
        }

        if (activity.getPackageManager().resolveActivity(v1, 0) != null) {
            String v2 = activity.getPackageManager().resolveActivity(v1, 0).activityInfo.packageName;
            if (v2 == null) {
                return;
            }

            if (v2.equals(activity.getPackageName())) {
                return;
            }

            try {
                activity.startActivity(Intent.createChooser(v1, activity.getResources().getString(R.string.open_with)));
            } catch (ActivityNotFoundException v3) {
            }
        }
    }

    public static void share(Context context, String msg) {
        Intent intent = new Intent("android.intent.action.SEND");
        intent.setType("text/plain");
        intent.putExtra("android.intent.extra.TEXT", msg);
        context.startActivity(Intent.createChooser(intent, context.getResources().getString(R.string.action_share)));
    }

    @SuppressLint(value = {"NewApi"})
    public boolean startActivityForUrl(WebView tab, String url) {
        Intent v1;
        int v9 = 15;
        boolean v4 = true;

        try {
            v1 = Intent.parseUri(url, 1);
            v1.addCategory("android.intent.category.BROWSABLE");
            v1.setComponent(null);
            if (Constants.API >= v9) {
                v1.setSelector(null);
            }
        } catch (URISyntaxException v0) {
            return false;
        }

        if (this.mActivity.getPackageManager().resolveActivity(v1, 0) == null) {
            String v3 = v1.getPackage();
            if (v3 != null) {
                v1 = new Intent("android.intent.action.VIEW", Uri.parse("market://search?q=pname:" +
                        v3));
                v1.addCategory("android.intent.category.BROWSABLE");
                v1.setComponent(null);
                if (Constants.API >= v9) {
                    v1.setSelector(null);
                }

                this.mActivity.startActivity(v1);
                return v4;
            }

            return false;
        }

        v1.addCategory("android.intent.category.BROWSABLE");
        v1.setComponent(null);
        if (Constants.API >= v9) {
            v1.setSelector(null);
        }

        if (tab != null) {
            v1.putExtra(this.mActivity.getPackageName() + ".Origin", 1);
        }

        if ((IntentUtils.ACCEPTED_URI_SCHEMA.matcher(((CharSequence) url)).matches()) && !IntentUtils.
                isSpecializedHandlerAvailable(this.mActivity, v1)) {
            return false;
        }

        try {
            if (this.mActivity.startActivityIfNeeded(v1, -1)) {
                return v4;
            }
        } catch (ActivityNotFoundException v4_1) {
        }

        return false;
    }
}

